module("ChipTool", package.seeall)



ChipTool = {}

ChipTool.chipNum = { 25, 50, 100, 500, 1000 }

--筹码下注用
--此方法为筹码_chipObg从_chipStartPos 飞行到 chipEndPos
--_toPosTransfrom  是下注区域的Transfrom  __isRandPos 为随机区域    _offset 为随机区域偏移量
function ChipTool:ChipMoveAni(_chipObg, _chipStartPos, _toPosTransfrom, _isRandPos, _aniEnd, _toPos, _index)
    function callback_start()

    end

    function callback_end()
        if _aniEnd then
            _aniEnd(_index)
        end
    end

    local tempV3
    if _isRandPos then
        tempV3 = Vector3(math.random(-_toPosTransfrom.width / 2 + 30, _toPosTransfrom.width / 2 - 30),
            math.random(-_toPosTransfrom.height / 2 + 30, _toPosTransfrom.height / 2 - 30), 0)


        tempV3 = _chipObg.transform.parent.transform:InverseTransformPoint(_toPosTransfrom.transform.position) + tempV3
    else
        tempV3 = _chipObg.transform.parent.transform:InverseTransformPoint(_toPos)
    end

    if _chipStartPos then
        _chipObg.transform.localPosition = _chipObg.transform.parent.transform:InverseTransformPoint(_chipStartPos)
    end
    local DOTweenMgr = require("UI.DOTweenMgr")
    self.closeTweener = DOTweenMgr.DTDOMove(
        _chipObg.transform,
        tempV3,
        0.5,
        1,
        false,
        callback_start,
        callback_end,
        0,
        3
    )
end

function ChipTool:ChipMoveStartPosRandomAni(_chipObg, _chipEndPos, _startPosTransfrom, _isRandPos, _aniEnd, _toPos, _index)
    function callback_start()

    end

    function callback_end()
        if _aniEnd then
            _aniEnd(_index)
        end
    end

    local tempV3
    if _isRandPos then
        tempV3 = Vector3(math.random(-_startPosTransfrom.width / 2 + 30, _startPosTransfrom.width / 2 - 30),
            math.random(-_startPosTransfrom.height / 2 + 30, _startPosTransfrom.height / 2 - 30), 0)


        tempV3 = _chipObg.transform.parent.transform:InverseTransformPoint(_startPosTransfrom.transform.position) + tempV3
    else
        tempV3 = _chipObg.transform.parent.transform:InverseTransformPoint(_toPos)
    end

    
        _chipObg.transform.localPosition = tempV3
     
    local DOTweenMgr = require("UI.DOTweenMgr")
    self.closeTweener = DOTweenMgr.DTDOMove(
        _chipObg.transform,
        _chipObg.transform.parent.transform:InverseTransformPoint(_chipEndPos),
        0.5,
        1,
        false,
        callback_start,
        callback_end,
        0,
        3
    )
end



--计算筹码
function ChipTool:analyzeChip(_value)
    local tempArr = {}
    for i = 1, #self.chipNum do
        if _value == self.chipNum[i] then
            tempArr[1] = _value
            return tempArr
        end
    end

    return self:analyzeChipFun(tempArr, #self.chipNum, _value)
end

function ChipTool:analyzeChipFun(arr, index, _value)
    if self.chipNum[index] <= _value then
        arr[#arr + 1] = self.chipNum[index]

        local tempValue = _value - self.chipNum[index]
        if tempValue == 0 then
            return arr
        else
            return self:analyzeChipFun(arr, index, tempValue)
        end
    else
        if index - 1 == 0 then
            return arr
        else
            return self:analyzeChipFun(arr, index - 1, _value)
        end
    end
end
